<?php
class LoginController extends Zendvn_Controller_Action{
	
    private $_arrParams;

    public function init(){
        $this->view->strMessages = $this->_helper->FlashMessenger->getMessages();
        $this->view->arrParam = $this->_request->getParams();
        $this->_arrParams = $this->_request->getParams();
    }

    public function indexAction(){
        //Init layout
        Zend_Layout::startMvc(array(
            'layout'     => 'login',
            'layoutPath' => PUBLIC_PATH . DS . 'layout'
        ));
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function checkloginAction(){
        if($this->_request->isPost()){
            //Get param
            $username = $this->_request->getParam('username');
            $password = $this->_request->getParam('password');
            if($username == null || $password == null){
                $this->_helper->FlashMessenger(array('title'=>'Đăng nhập lỗi','content'=>'Vui lòng kiểm tra lại thông tin đăng nhập'));
                $this->_redirect(BASE_URL .'/default/login');
            }            
            $remember = $this->_request->getParam('remember');
            
            //Check login
            $modeluser = Default_Model_User::getInstance();
            $detailUser = $modeluser->checkLoginUser($username,md5($password));
            if($detailUser){
                //True : Write session
                $session = Core_Session::getInstance('user');
                $session->username  = $detailUser['username'];
                $session->first_name  = $detailUser['first_name'];
                $session->last_name  = $detailUser['last_name'];
                $session->full_name  = $detailUser['first_name'] .' '.$detailUser['last_name'];
                $session->avatar  = $detailUser['avatar'];
                $session->group  = $detailUser['group'];
                //Check remember
                if($remember){
                    $cookie_domain = ".".$_SERVER['HTTP_HOST'];
                    setcookie('computer_user',json_encode($detailUser),time()+(86400*3),'/',$cookie_domain);
                    $detailUser = (array)json_decode($_COOKIE['computer_user']);
                }
                $this->_redirect(BASE_URL);
            }else{
                //Wrong
                $this->_helper->FlashMessenger(array('title'=>'Đăng nhập lỗi','content'=>'Vui lòng kiểm tra lại thông tin đăng nhập'));
                $this->_redirect(BASE_URL .'/default/login');
            }
        }
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function logoutAction(){
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout()->disableLayout();
        $cookie_domain = ".".$_SERVER['HTTP_HOST'];
        setcookie('computer_user','',time()-3600,'/',$cookie_domain);
        Zend_Session::destroy();
        $this->_redirect(BASE_URL .'/default/login');
    }
}